package com.superhelper.receiver.web.controller;

import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.superhelper.common.Consts;
import com.superhelper.common.domain.dada.DaDaCallback;
import com.superhelper.common.service.AliMQProducerService;

/**
 */
@Controller
@RequestMapping("/dada")
public class DaDaCallbackController {
    protected Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AliMQProducerService producer;

    @RequestMapping("/callback2")
    @ResponseBody
    public String callback2() {
        // recordMapper.updateStatusByStatusOrder(1000002L, 3, "22", "", "");
        return "aa";
    }

    @RequestMapping("/callback")
    @ResponseBody
    public String callback(@RequestBody DaDaCallback cb, HttpServletRequest request, HttpServletResponse response) {

        log.info("callback {}", cb);
        // unsend("未发单", 0), waiting("待接单", 1), picking("待取货", 2),
        // delivering("配送中", 3), completed("已完成", 4), canceled("已取消",
        // 5), expired("已过期", 7), assigned("指派单", 8), abnormal("异常单", -1);
        // client_id, order_id, update_time
        TreeSet<String> set = new TreeSet<>();
        set.add(cb.getClient_id());
        set.add(cb.getOrder_id());
        set.add(cb.getUpdate_time() + "");
        StringBuffer sb = new StringBuffer();
        for (String str : set) {
            sb.append(str);
        }
        String mySign = DigestUtils.md5DigestAsHex(sb.toString().getBytes(Consts.UTF8_CHARSET));
        String mySign2 = DigestUtils
                .md5DigestAsHex(sb.append("e84f2849d2218ba2d416738f683bb6ba").toString().getBytes(Consts.UTF8_CHARSET));
        log.info("mySign:{},mySign2:{}", mySign, mySign2);
        producer.sendDada(cb);
        /*
         * try {
         * DeliverRecordStatus drs =
         * DeliverRecordStatus.valueOf(cb.getOrder_status());
         * if (cb.getOrder_status() == DeliverRecordStatus.completed.getVal()) {
         * DeliverRecord dr =
         * recordMapper.getById(NumberUtils.toLong(cb.getOrder_id()));
         * if (dr != null) {
         * recordMapper.updateStatusByStatusOrder(dr.getId(),
         * cb.getOrder_status(), drs.getDesc(),
         * cb.getDm_name(), cb.getDm_mobile());
         * orderMapper.updateSettledById(dr.getOrderId());
         * }
         * 
         * } else if (cb.getOrder_status() ==
         * DeliverRecordStatus.picking.getVal()) {
         * recordMapper.updatePickingById(NumberUtils.toLong(cb.getOrder_id()),
         * drs.getDesc(), cb.getDm_name(),
         * cb.getDm_mobile());
         * } else if (cb.getOrder_status() ==
         * DeliverRecordStatus.delivering.getVal()) {
         * DeliverRecord dr =
         * recordMapper.getById(NumberUtils.toLong(cb.getOrder_id()));
         * if (dr != null) {
         * if (0 < recordMapper.updateStatusByStatusOrder(dr.getId(),
         * cb.getOrder_status(), drs.getDesc(),
         * cb.getDm_name(), cb.getDm_mobile())) {
         * orderMapper.updateDeliveringById(dr.getOrderId());
         * }
         * }
         * } else {
         * String statusMsg = drs.getDesc();
         * if (drs == DeliverRecordStatus.canceled) {
         * statusMsg = cb.getCancel_reason();
         * }
         * recordMapper.updateStatusNotComplete(NumberUtils.toLong(cb.
         * getOrder_id()), cb.getOrder_status(),
         * statusMsg);
         * 
         * }
         * } catch (Throwable e) {
         * log.error("error", e);
         * }
         */
        response.setStatus(200);
        return "success";
    }
}
